package com.wealth.scsj.p_app.view.activity.payment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wealth.scsj.p_app.R;
import com.wealth.scsj.p_app.adapter.WithDrawalAdapter;
import com.wealth.scsj.p_app.bean.BalanceBean;
import com.wealth.scsj.p_app.mydefinedview.CommonPopupWindow;
import com.wealth.scsj.p_app.mydefinedview.datepickers.DatePicker;
import com.wealth.scsj.p_app.utils.SkipUtils;
import com.wealth.scsj.p_app.view.activity.BaseActivity;
import java.util.ArrayList;
import java.util.List;

import es.dmoral.toasty.Toasty;

/**
 * 账单记录
 */
public class WithdrawalRecordActivity extends BaseActivity implements View.OnClickListener, CommonPopupWindow.ViewInterface {

    /**
     * 本月
     */
    private TextView mTopDateTv;
    /**
     * 提现成功
     */
    private ImageView top_date_img;
    private RecyclerView mRecyclerview;
    private CommonPopupWindow popupWindow;
    /**
     * 审核中
     */
    private RelativeLayout mReviewingTv;
    /**
     * 提现成功
     */
    private RelativeLayout mWithdrawalSucceedTv;
    /**
     * 提现失败
     */
    private RelativeLayout mWithdrawalDefeatedTv;
    /**
     * 最外层父布局
     */
    private LinearLayout parentLayout;
    private TextView toolbar_title_right;
    private ImageView toolbar_title_rifht_img;


    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_withdrawal_record);
//        ScreenAdapterTools.getInstance().loadView((ViewGroup) getWindow().getDecorView());
        //ScreenAdapterTools.getInstance().reset(this);//如果希望android7.0分屏也适配的话,加上这句
        initToolbar();
        setToolbarTitle("账单记录");
        hasBack(true);
        initView();

    }

    @Override
    protected void initView() {

        mTopDateTv = (TextView) findViewById(R.id.top_date_tv);
        mTopDateTv.setOnClickListener(this);
        mRecyclerview = (RecyclerView) findViewById(R.id.recyclerview);
        parentLayout= findViewById(R.id.parent_layout);
        top_date_img = findViewById(R.id.top_date_img);
        top_date_img.setOnClickListener(this);

        toolbar_title_right = (TextView) findViewById(R.id.toolbar_title_right);
        toolbar_title_right.setVisibility(View.VISIBLE);
        toolbar_title_right.setOnClickListener(this);
        toolbar_title_right.setText("筛选");

        toolbar_title_rifht_img = (ImageView) findViewById(R.id.toolbar_title_rifht_img);
        toolbar_title_rifht_img.setVisibility(View.VISIBLE);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        List mBalance = new ArrayList<BalanceBean>();
        mBalance.add(new BalanceBean("购物消费", "2016-6-6  16:6:6", 666.66));
        mBalance.add(new BalanceBean("采购商成消费", "2016-6-7  16:6:6", 666.66));
        mBalance.add(new BalanceBean("实体店消费", "2016-6-8  16:6:6", 666.66));
        mBalance.add(new BalanceBean("购物消费", "2016-6-9  16:6:6", -666.66));
        mBalance.add(new BalanceBean("采购商成消费", "2016-6-0  16:6:6", 666.66));
        mBalance.add(new BalanceBean("购物消费", "2016-6-1  16:6:6", 666.66));
        mBalance.add(new BalanceBean("购物消费1", "2016-6-2  16:6:6", -666.66));
        mBalance.add(new BalanceBean("购物消费2", "2016-6-3  16:6:6", 666.66));
        mBalance.add(new BalanceBean("购物消费", "2016-6-4  16:6:6", 666.66));
        mBalance.add(new BalanceBean("购物消费3", "2016-6-5  16:6:6", 666.66));
        mBalance.add(new BalanceBean("购物消费5", "2016-6-6  16:6:6", 666.66));
        mBalance.add(new BalanceBean("购物消费6", "2016-6-6  16:6:6", -666.66));
        mBalance.add(new BalanceBean("购物消费7", "2016-6-6  16:6:6", 666.66));
        mBalance.add(new BalanceBean("购物消费8", "2016-6-6  16:6:6", 666.66));
        mBalance.add(new BalanceBean("购物消费9", "2016-6-6  16:6:6", 666.66));
        mBalance.add(new BalanceBean("购物消费1", "2016-6-6  16:6:6", 666.66));
        WithDrawalAdapter balanceAdapter = new WithDrawalAdapter(mBalance);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
//        mRecyclerview.addItemDecoration(new MyDecoration(this, LinearLayoutManager.HORIZONTAL));
        mRecyclerview.setAdapter(balanceAdapter);
        balanceAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                // 添加跳转，之后需要传递对应的position
                SkipUtils.toBillDetailsPage(WithdrawalRecordActivity.this);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.top_date_img:
                datePicker();
                break;
            case R.id.toolbar_title_right:
                showDownPop();
                break;
            case R.id.reviewing_tv:
                Toasty.normal(this,"审核中……").show();
                break;
            case R.id.withdrawal_succeed_tv:
                Toasty.normal(this,"提现成功……").show();

                break;
            case R.id.withdrawal_defeated_tv:
                Toasty.normal(this,"提现失败……").show();
                break;
        }
    }

    private void datePicker() {
        DatePicker picker = new DatePicker(this, DatePicker.YEAR_MONTH);
        picker.setGravity(Gravity.BOTTOM);
//                picker.setWidth((int) (picker.getScreenWidthPixels() * 0.6));
        picker.setRangeStart(2016, 10, 14);
        picker.setRangeEnd(2020, 11, 11);
        picker.setSelectedItem(2017, 9);
        picker.setOnDatePickListener(new DatePicker.OnYearMonthPickListener() {
            @Override
            public void onDatePicked(String year, String month) {
                mTopDateTv.setText(year + "年" + month + "号");
            }
        });
        picker.show();
    }

    //向下弹出
    public void showDownPop() {

//        if (popupWindow != null && popupWindow.isShowing()) return;
        popupWindow = new CommonPopupWindow.Builder(this)
                .setView(R.layout.item_pop_withdrawal_record)
                .setWidthAndHeight(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                //.setAnimationStyle(R.style.AnimDown)
                .setViewOnclickListener(this)
                .setOutsideTouchable(true)
                //设置背景颜色，取值范围0.0f-1.0f 值越小越暗 1.0f为透明
                .setBackGroundLevel(0.5f)

                //设置PopupWindow里的子View及点击事件
                .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                    @Override
                    public void getChildView(View view, int layoutResId) {
                        RelativeLayout tv_child1 = (RelativeLayout) view.findViewById(R.id.reviewing_tv);
                        tv_child1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Toasty.normal(WithdrawalRecordActivity.this,"111按钮点击").show();
                            }
                        });

                        RelativeLayout tv_child2 = (RelativeLayout) view.findViewById(R.id.withdrawal_succeed_tv);
                        tv_child2.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Toasty.normal(WithdrawalRecordActivity.this,"222按钮点击").show();

                            }
                        });

                        RelativeLayout tv_child3 = (RelativeLayout) view.findViewById(R.id.withdrawal_defeated_tv);
                        tv_child3.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Toasty.normal(WithdrawalRecordActivity.this,"333按钮点击").show();

                            }
                        });

                    }
                })
                .create();
        popupWindow.showAsDropDown(toolbar_title_right,150,30);

    }

    @Override
    public void getChildView(View view, int layoutResId) {

        switch (layoutResId) {
            case R.layout.item_pop_withdrawal_record:
                mReviewingTv =  view.findViewById(R.id.reviewing_tv);
                mReviewingTv.setOnClickListener(this);
                mWithdrawalSucceedTv =  view.findViewById(R.id.withdrawal_succeed_tv);
                mWithdrawalSucceedTv.setOnClickListener(this);
                mWithdrawalDefeatedTv =  view.findViewById(R.id.withdrawal_defeated_tv);
                mWithdrawalDefeatedTv.setOnClickListener(this);
                break;
        }
    }
}
